/** Part of Zombie Navigator
	Copyright © 2016  Zombie Navigator Developers
	
	@file
	@license AGPL-3.0+
	
*/ 'use strict';

self.port.once('capture', useWindow => {
	try {
		let left = 0, top = 0, bgColor = 'rgba(0,0,0,0)';
		let width = frames.innerWidth, height = frames.innerHeight;
		const origX = frames.scrollX, origY = frames.scrollY;
		
		if (useWindow) {
			left = window.scrollX;
			top = window.scrollY;
		} else {
			scrollTo(0, 0);
			if (window.scrollMaxX) {
				width += window.scrollMaxX - window.scrollMinX;
				height += window.scrollMaxY - window.scrollMinY;
			} else {
				width = window.document.documentElement.scrollWidth;
				height = window.document.documentElement.scrollHeight;
			}
		}
		
		const canvas = window.document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
		const ratio = window.devicePixelRatio;
		canvas.width = width * ratio;
		canvas.height = height * ratio;
		const ctx = canvas.getContext('2d');
		ctx.scale(ratio, ratio);
		ctx.drawWindow(frames, left, top, width, height, bgColor);
		
		const dataURI = canvas.toDataURL('image/png', '');
		if (!useWindow) scrollTo(origX, origY);
		
		self.port.emit('cData', dataURI);
	} catch (e) {
		self.port.emit('cError', '' + e);
	}
});

// vim: ts=4 noet ai

